﻿using System;

namespace MenusHunter.Services.Utilities
{
    public static class Utils
    {
        public static double CalcDistance2Point(double lat1, double lon1, double lat2, double lon2)
        {
            const int r = 6371; //km -- ban kinh trai dat
            var dLat = Math.Abs(lat2 - lat1).ToRad();
            var dLon = Math.Abs(lon2 - lon1).ToRad();
            lat1 = lat1.ToRad();
            lat2 = lat2.ToRad();

            var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
                    Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
            var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            return r * c;
        }

        private static double ToRad(this double a)
        {
            return a * Math.PI / 180;
        }
    }
}
